'use strict';

(function($){

    $(document).ready(function() {

        /*导航子菜单hover显示与隐藏*/
        $('#header .menu-item-has-children').hover(function() {
            if ($(window).width() >= 1200)
                $(this).children('ul').stop().show();
        }, function() {
            if ($(window).width() >= 1200)
                $(this).children('ul').stop().hide();
        });
        
        $('.details').width($(window).width());
        $(window).resize(function(event) {
            $('.details').width($(window).width()); 
        });

        /* 菜单按钮 */
        $('#menu-toggle').click(function(event) {
            $('#menu').slideToggle( 'fast' );
        });

        // 菜单向下滚动隐藏
        var scrollValue = $(window).scrollTop();
        var header_top_height = $('.header_top').outerHeight();
            outerHeight = $("#header").outerHeight();

        // 回到顶部
        var btn = document.getElementById('back-top');
        var clientHeight = $(window).height();

        $('#back-top').on('click', function(event) {
            event.preventDefault();
            $('html,body').animate({
                scrollTop: '0'
            }, 500);
        });

        window.onscroll = function(){
            
            // 菜单向下滚动隐藏
            var scrollTop = $(window).scrollTop();
            if(scrollTop > outerHeight + header_top_height && scrollTop > scrollValue){
                $("#header").css('top', -outerHeight);
            } else if (scrollTop == 0) {
                $('#header').removeClass('scroll')
            }else{
                scrollTop + $(window).height() < $(document).height() && $("#header").addClass('scroll').css('top', '0px').show(400);
            }
            scrollValue = scrollTop;

            // 回到顶部
            var ostop = document.documentElement.scrollTop||document.body.scrollTop;
            if (ostop>clientHeight) {
                btn.style.display = 'block';
            } else{
                btn.style.display = 'none';
            };

        }

        /*控制台打印招聘信息*/
        // console.log( '%c Welinkcredit is hiring!\n %c 给我们投简历：service@welinkcredit.com', 'font-size:60px;color:#337ab7', 'font-size:20px;color:#333;' );

        // color box below
        
        $('<div id="color-box" style="position:fixed;top:0;right:0;bottom:0;left:0;display:none;background:#ccc;opacity: 0.5;z-index:101;filter:blur(5px);"></div>').appendTo('body');

        $('body').on('click', '#color-box', function(event) {
            $('#color-box').fadeOut(500);
            $('.color-box-img').hide(500);
        });

        $('.article img').each(function(index, el) {
            $(el).click(function(event) {
                $(event.target).clone().appendTo('body').css({
                    position: 'fixed',
                    top: '50%',
                    left: '50%',
                    right: '0',
                    bottom: '0',
                    zIndex: '102',
                    transform: 'translate(-50%,-50%)',
                    maxWidth: '90%',
                    maxHeight: '90%',
                    width: 'auto',
                    height: 'auto',
                    display: 'none'
                }).attr('class', 'color-box-img').show(500);
                $('#color-box').fadeIn(500);
            });
        });

    });

})(jQuery);

// add bookmark
function AddFavorite(){
    var sURL = encodeURI(window.location);
    var sTitle = document.title;
    try{
        window.external.addFavorite(sURL, sTitle);
    }catch (e){
        try{
            window.sidebar.addPanel(sTitle, sURL, "");
        }catch (e){
            alert("抱歉，您的浏览器不支持此项功能，请手动设置。");
        }
    }
}

// set home
function SetHome(){
    var url = window.location;
    if (document.all){
        document.body.style.behavior = 'url(#default#homepage)';
        document.body.setHomePage(url);
    }else{
        alert("抱歉，您的浏览器不支持此项功能，请手动设置。");
    }
    
}